package org.zn.mvcbase.config;



import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.zn.mvcbase.config.property.BootMvcProperty;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

/**  
*
* @Company OBOR 
* @author 周楠
* @Description: 
* spring boot 1.5.xx  配置 thymeleaf 3
* @date 2018年2月7日
*
*/
@Configuration
@EnableWebMvc
@ComponentScan("com.thymeleafexamples")
@ConditionalOnProperty(value="bootmvc.thymeleaf",matchIfMissing=false)
@EnableConfigurationProperties(value =  {BootMvcProperty.class})
public class ThymeLeaf3Config  extends WebMvcConfigurerAdapter implements ApplicationContextAware{
	
//	 private String   prefix="/templates/";
//	 private String   suffix=".html";
//	 private String   encoding="UTF-8";
//	 private String  content_type="text/html";
//	 private String  cache="false";      
	
	private static transient final Logger log = LoggerFactory.getLogger(ThymeLeaf3Config.class);
	
	 @Autowired
	 private BootMvcProperty bootMvcProperty;
	 
	 private ApplicationContext applicationContext;
	 
	 
	 @PostConstruct
	public void initLog() {
			
		log.info("{} init","ThymeLeaf3Config");
			
	}
	

	public void setApplicationContext(ApplicationContext applicationContext) {
	    this.applicationContext = applicationContext;
	  }
	
	
	   @Override
	    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	        registry.addResourceHandler("/**").addResourceLocations("/");
	    }


	    @Override
	    public void configureDefaultServletHandling(
	            DefaultServletHandlerConfigurer configurer) {
	        configurer.enable();
	    }

	  @Bean
	  public ViewResolver viewResolver() {
	    ThymeleafViewResolver resolver = new ThymeleafViewResolver();


	    TemplateEngine templateEngine=templateEngine();
	    
//	    resolver.setTemplateEngine(templateEngine);
	    
	  
	    resolver.setCharacterEncoding(bootMvcProperty.getThymeleaf().getEncoding());
	    
	    resolver.setCache(new Boolean(bootMvcProperty.getThymeleaf().getCache()));
	    resolver.setContentType(bootMvcProperty.getThymeleaf().getContent_type());

	    return resolver;
	  }

	  private TemplateEngine templateEngine() {
	    SpringTemplateEngine engine = new SpringTemplateEngine();
	    
	    engine.setTemplateResolver(templateResolver());
	    
	    return engine;
	  }

	  
	  private ITemplateResolver templateResolver() {
	    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
	    resolver.setApplicationContext(applicationContext);
	    resolver.setPrefix(bootMvcProperty.getThymeleaf().getPrefix());
	    resolver.setSuffix(bootMvcProperty.getThymeleaf().getSuffix()); 
// 	    resolver.setTemplateMode(TemplateMode.HTML);
	    return resolver;
	  }
	  
	  @Bean
	  public ShiroDialect shiroDialect() {
		return new ShiroDialect();
	  }

	  
	}
